import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"
import axios from "axios"

interface ShoppingCartState {
  loading: boolean,
  error: string | null,
  items: any[]
}

const initialState: ShoppingCartState = {
  loading: true,
  error: null,
  items: []
}

// 获取购物车
export const getShoppingCart = createAsyncThunk(
  'shoppingCart/getShoppingCart',
  async (jwt: string, thunkAPI) => {
    let url = `http://82.157.43.234:8080/api/shoppingCart`
    const { data } = await axios.get(url, {
      headers: {
        'x-icode': 'EDE45279EB142953',
        Authorization: 'Bearer ' + jwt
      }
    })
    return data.shoppingCartItems

  }
)
// 添加购物车
export const addShoppingCartItem = createAsyncThunk(
  'shoppingCart/addShoppingCartItem',
  async (parameters: { jwt: string, touristRouteId: string }, thunkAPI) => {
    let url = `http://82.157.43.234:8080/api/shoppingCart/items`
    const { data } = await axios.post(url, {
      touristRouteId: parameters.touristRouteId
    }, {
      headers: {
        'x-icode': 'EDE45279EB142953',
        Authorization: 'Bearer ' + parameters.jwt
      }
    })
    return data.shoppingCartItems

  }
)
// 删除购物车
export const clearShoppingCartItem = createAsyncThunk(
  'shoppingCart/clearShoppingCartItem',
  async (parameters: { jwt: string, itemIds: number[] }, thunkAPI) => {
    let url = `http://82.157.43.234:8080/api/shoppingCart/items/${parameters.itemIds.join(',')}`
    return await axios.delete(url, {
      headers: {
        'x-icode': 'EDE45279EB142953',
        Authorization: 'Bearer ' + parameters.jwt
      }
    })

  }
)
// 下单
export const checkout = createAsyncThunk(
  'shoppingCart/checkout',
  async (jwt: string, thunkAPI) => {
    let url = `http://82.157.43.234:8080/api/shoppingCart/checkout`
    const { data } = await axios.post(url, null, {
      headers: {
        'x-icode': 'EDE45279EB142953',
        Authorization: 'Bearer ' + jwt
      }
    })
    return data

  }
)


export const shoppingCartSlice = createSlice({
  name: 'shoppingCart',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(getShoppingCart.pending, (state) => {
        state.loading = true
      })
      .addCase(getShoppingCart.fulfilled, (state, action) => {
        state.items = action.payload
        state.loading = false
        state.error = null
      })
      .addCase(getShoppingCart.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }
      })
      .addCase(addShoppingCartItem.pending, (state) => {
        state.loading = true
      })
      .addCase(addShoppingCartItem.fulfilled, (state, action) => {
        state.items = action.payload
        state.loading = false
        state.error = null
      })
      .addCase(addShoppingCartItem.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }
      })
      .addCase(clearShoppingCartItem.pending, (state) => {
        state.loading = true
      })
      .addCase(clearShoppingCartItem.fulfilled, (state) => {
        state.items = []
        state.loading = false
        state.error = null
      })
      .addCase(clearShoppingCartItem.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }
      })
      .addCase(checkout.pending, (state) => {
        state.loading = true
      })
      .addCase(checkout.fulfilled, (state) => {
        state.items = []
        state.loading = false
        state.error = null
      })
      .addCase(checkout.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }
      })
  }
})